<?php
/**
*  Create On 2014-12-18
*  Author yiwei
*  QQ:1006629314
**/
class message extends Model{
	private $table = 'xdf_message';
	
	public function addMessage($type,$argv,$desc,$sender,$receiver){
		if ($type == 3){
			return true;
		}
		$sql = 'INSERT INTO `'.$this->table.'` (`type`,`argv`,`desc`,`sender`,
				`receiver`,`create_time`) VALUES ("'.$type.'","'.$argv.'","'.$desc.'",
				"'.$sender.'","'.$receiver.'",NOW())';
		return $this->insert($sql);
	} 
	
	//用户收到的信息列表
	public function receiveMessage($user_id,$type = 0){
		$sql = 'SELECT * FROM `'.$this->table.'` WHERE `receiver` = '.$user_id;
		$filter = ' AND `checked` >=0';
		if($type){
			$filter .=' AND `type` = '.$type;
		}
		$order = ' ORDER BY `checked` DESC,`create_time` DESC';
		//echo $sql.$filter.$order;
		return $this->fetchAll($sql.$filter.$order);
	}

	//获取我的一条系统消息
	public function getMyMessageById($user_id,$id){
		$sql = "SELECT * FROM `$this->table`
		WHERE id = '$id' AND `receiver`='$user_id';";
		return $this->fetchRow($sql);
	}

	
	//改变用户操作信息的状态,type :1 已看过,0:未看过,-1:隐藏（删除）
	public function checkMessage($id,$type){
		$sql = "UPDATE `$this->table` SET `checked` = '$type'
				WHERE id = '$id';";
		return $this->update($sql);
	}
	//删除所有消息
	public function checkAllMessage($user_id,$type){
		$sql = 'UPDATE `'.$this->table.'` SET `checked` = '.$type.'
				WHERE receiver = '.$user_id;
		return $this->update($sql);
	}

	//检查用户是否在某个资源上是否有信息未查看
	public function getMessage($type,$argv){
		$sql = "SELECT * FROM `$this->table` 
				WHERE  `type` = '$type' AND `argv`='$argv';";
		return $this->fetchAll($sql);
	}
	//检查用户是否在某个资源上是否有信息未查看
	public function hasSentMessage($user_id,$type,$argvList){
		$sql = 'SELECT `id` FROM `'.$this->table.'` WHERE `sender` = '.$user_id.'
				AND `type` = '.$type.' AND `argv` IN ('.str_replace('_', ',', $argvList).')';
		return $this->fetchRow($sql);
	}
	//检查用户是否在某个资源上是否有信息未查看
	public function hasMessage($user_id,$type,$argvList){
		$sql = 'SELECT `id` FROM `'.$this->table.'` WHERE `reveiver` = '.$user_id.'
				AND `type` = '.$type.' AND `argv` IN ('.str_replace('_', ',', $argvList).')';
		return $this->fetchAll($sql);
	}
	//获取我的系统通知，分页实现
	public function getReceivedMessageList($user_id,$timestamp,$pagesize,$action){
	// 	if($action=='pre')//刷新
			$preOrMore_sql="";
		if($action=='more'){//获取更多
			$preOrMore_sql="AND id < $timestamp";
		}
		$sql="SELECT * FROM $this->table WHERE `checked`>=0 AND
			`receiver`='$user_id' $preOrMore_sql ORDER BY `id` DESC LIMIT $pagesize";
//  		var_dump($sql);
		return $this->fetchAll($sql);
	}
	public function getPushedMessageList($user_id,$timestamp,$pagesize,$action){
	// 	if($action=='pre')//刷新
			$preOrMore_sql="";
		if($action=='more'){//获取更多
			$preOrMore_sql="AND id < $timestamp";
		}
		$sql="SELECT * FROM $this->table WHERE `serder`='$user_id' $preOrMore_sql ORDER BY `id` DESC ";
		return $this->fetchAll($sql);
		
	}
	//获取新消息条数
	public function getUserfulMessageTotal($user_id){
		return $this->getTotal($this->table,"`receiver`='$user_id' AND checked = '0'");
	}
	
}